package com.liao.starapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.liao.starapp.luck.Luck;
import com.liao.starapp.partner.PartnerActivity;
import com.liao.starapp.utils.GetAssetsData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class FragmentPartnerLayoutFragment extends Fragment implements View.OnClickListener {

    private Context context;

    private ImageView img_boy;
    private Spinner spinner_boy;
    private ImageView img_girl;
    private Spinner spinner_girl;
    private Button btn_begin;
    private Button btn_reward;

    private ArrayList<Luck> lucks;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_partner_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        context = getContext();
        img_boy = view.findViewById(R.id.img_boy);
        img_girl = view.findViewById(R.id.img_girl);
        spinner_boy = view.findViewById(R.id.spinner_boy);
        spinner_girl = view.findViewById(R.id.spinner_girl);
        btn_begin = view.findViewById(R.id.btn_begin);
        btn_reward = view.findViewById(R.id.btn_reward);

        btn_begin.setOnClickListener(this);
        btn_reward.setOnClickListener(this);

        lucks = new ArrayList<>();
        getData();
        String[] names = new String[lucks.size()];
        for (int i = 0; i < lucks.size(); i++) {
            names[i] = lucks.get(i).getName();
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_partner_layout, names);
        spinner_boy.setAdapter(adapter);
        spinner_girl.setAdapter(adapter);
        spinner_boy.setOnItemSelectedListener(new MySpinnerBoyListener());
        spinner_girl.setOnItemSelectedListener(new MySpinnerGirlListener());
    }

    private void getData() {
        String text = GetAssetsData.getGridViewText(context, "yunshis/yunshi.json");
        json(text);
    }

    private void json(String s) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String logoname = jsonObject.getString("logoname");
                String name = jsonObject.getString("name");
                Luck luck = new Luck();
                luck.setLogoname(logoname);
                luck.setName(name);
                lucks.add(luck);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reward:
                Toast.makeText(context, "抽奖", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_begin:
                Luck luckBoy = lucks.get(spinner_boy.getSelectedItemPosition());
                Luck luckGirl = lucks.get(spinner_girl.getSelectedItemPosition());
//                Toast.makeText(context, "男生星座：" + boyName + "===女生星座" + girlName, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(context, PartnerActivity.class)
                        .putExtra("boy", luckBoy).putExtra("girl", luckGirl));
                break;
        }
    }

    private class MySpinnerBoyListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Bitmap bitmap = GetAssetsData.getGridViewImg(context, "xzcontentlogo/" + lucks.get(position).getLogoname() + ".png");
            img_boy.setImageBitmap(bitmap);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }

    private class MySpinnerGirlListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Bitmap bitmap = GetAssetsData.getGridViewImg(context, "xzcontentlogo/" + lucks.get(position).getLogoname() + ".png");
            img_girl.setImageBitmap(bitmap);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }
}
